/**
 * @file getMail.c 
 * @author Xiangrui Meng & Yuqi Liu
 * @brief 获取邮件的content,base64转码
 * @version 0.1
 * @date 2022-06-3
 *
 * @copyright Copyright (c) 2022
 *
 */
#include "divMail.c"
#include "base64_converter.c"

int getMail(int sock);

int getMail(int sock)
{
    char plain[1024];
    char header[1024];
    char html[1024];
    char mail[4096];
    memset(plain, 0, strlen(plain));
    memset(html, 0, strlen(html));
    memset(mail, 0, strlen(mail));

    char *recTemp;
    int resp;
    // email to display
    printf("mypop > Which email to read: \n");
    scanf("%d", &resp);
    CLEAR_BUF
    if (resp>listNum(sock)||listNum<=0)
    {
        perror("The email does not exist!!! Please try again");
        return 1;
    }
    
    recTemp = getDetail(sock, resp);

    //拿到邮件headers
    char *headerTemp;
    headerTemp = divMail(recTemp, 1);
    strcpy(header, headerTemp);


    //拿到邮件纯文本内容base64
    char *plainTemp;
    plainTemp = divMail(recTemp, 2);
    strcpy(plain, plainTemp);

    //拿到邮件html内容 base64
    char *htmlTemp;
    htmlTemp = divMail(recTemp, 3);
    strcpy(html, htmlTemp);

    //拆分每行
    char seg[] = "\r\n";
    char *token1;
    char *token2;
    char *decode_plain;
    char *decode_html;
    char header_plain[1024];
    char content_plain[1024];
    char header_html[1024];
    char content_html[1024];
    // memset(content_plain, 0, strlen(content_plain));
    // memset(content_html, 0, strlen(content_html));

    // get plain content
    int count1 = 0;
    token1 = strtok(plain, seg);
    strcat(header_plain, token1); //拼贴第一句
    strcat(header_plain, "\r\n");
    while ((token1 = strtok(NULL, seg)) != NULL)
    {
        count1++;
        if (count1 < 2)
        {
            strcat(header_plain, token1);
            strcat(header_plain, "\r\n");
        }
        if (count1 >= 2)
        {
            // base64转ascii
            decode_plain = base64_decode((unsigned char *)token1);
            strcat(content_plain, decode_plain);
        }
    }

    // get html content
    int count2 = 0;
    memset(content_html, 0, strlen(content_html));
    memset(header_html, 0, strlen(header_html));
    token2 = strtok(html, seg);
    strcat(header_html, token2); //拼贴第一句
    strcat(header_html, "\r\n");
    while ((token2 = strtok(NULL, seg)) != NULL)
    {
        count2++;
        if (count2 < 2)
        {
            strcat(header_html, token2);
            strcat(header_html, "\r\n");
        }
        if (count2 >= 2)
        {
            decode_html = base64_decode((unsigned char *)token2);
            strcat(content_html, decode_html);
            // strcat(content_html, "\r\n");
        }
    }

    strcpy(mail, header_plain);
    strcat(mail, "\r\n");
    strcat(mail, content_plain);
    strcat(mail, "\r\n");
    strcat(mail, header_html);
    strcat(mail, "\r\n");
    strcat(mail, content_html);

    printf("mypop > Your mail:\n");
    printf("%s", header);
    printf("%s", mail);
    
    memset(header_plain, 0, strlen(header_plain));
    memset(decode_plain, 0, strlen(decode_plain));
    memset(content_plain, 0, strlen(content_plain));
    memset(content_html, 0, strlen(content_html));
    memset(header_html, 0, strlen(header_html));
    memset(decode_html, 0, strlen(decode_html));
}
